package com.huawei.demo;

// 用户身份认证
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
// 请求异常类
import com.huaweicloud.sdk.core.exception.ClientRequestException;
// 导入待请求接口的 request 和 response 类
import com.huaweicloud.sdk.organizations.v1.OrganizationsClient;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationalUnitReqBody;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationalUnitRequest;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationalUnitResponse;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationalUnitRequest;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationalUnitResponse;
import com.huaweicloud.sdk.organizations.v1.model.ListRootsRequest;
import com.huaweicloud.sdk.organizations.v1.model.ListRootsResponse;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationalUnitRequest;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationalUnitResponse;
import com.huaweicloud.sdk.organizations.v1.model.UpdateOrganizationalUnitReqBody;
import com.huaweicloud.sdk.organizations.v1.model.UpdateOrganizationalUnitRequest;
import com.huaweicloud.sdk.organizations.v1.model.UpdateOrganizationalUnitResponse;
// 日志打印
import com.huaweicloud.sdk.organizations.v1.region.OrganizationsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// 帐号管理
public class OrganizationsUnitManagementDemo {

    private static final Logger logger = LoggerFactory.getLogger(OrganizationsUnitManagementDemo.class.getName());

    public static void main(String[] args) {
        // 创建认证
        String ak = "{your ak string}";
        String sk = "{your sk string}";

        ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk);

        OrganizationsClient organizationsClient =
            OrganizationsClient.newBuilder()
                .withCredential(auth)
                .withRegion(OrganizationsRegion.valueOf("cn-north-4"))
                .build();

        OrganizationsUnitManagementDemo demo = new OrganizationsUnitManagementDemo();
        // 1、创建组织
        demo.createOrganization(organizationsClient);
        // 2、查询所属组织信息
        demo.showOrganization(organizationsClient);
        // 3、列出组织的根
        ListRootsResponse listRootsResponse = demo.listRoots(organizationsClient);
        // 4、创建组织单元
        CreateOrganizationalUnitResponse createOrganizationalUnitResponse =
            demo.createOrganizationalUnit(organizationsClient, listRootsResponse.getRoots().get(0).getId());
        // 5、更改组织单元名称
        demo.updateOrganizationalUnit(organizationsClient,
            createOrganizationalUnitResponse.getOrganizationalUnit().getId());
        // 6、查询有关组织单元的信息
        demo.showOrganizationalUnit(organizationsClient,
            createOrganizationalUnitResponse.getOrganizationalUnit().getId());
        // 7、删除组织单元
        demo.deleteOrganizationalUnit(organizationsClient,
            createOrganizationalUnitResponse.getOrganizationalUnit().getId());
        // 8、删除组织
        demo.deleteOrganization(organizationsClient);

    }

    public void createOrganization(OrganizationsClient organizationsClient) {
        CreateOrganizationRequest createOrganizationRequest = new CreateOrganizationRequest();
        try {
            CreateOrganizationResponse createOrganizationResponse =
                organizationsClient.createOrganization(createOrganizationRequest);
            logger.info(createOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void showOrganization(OrganizationsClient organizationsClient) {
        ShowOrganizationRequest showOrganizationRequest = new ShowOrganizationRequest();
        try {
            ShowOrganizationResponse showOrganizationResponse =
                organizationsClient.showOrganization(showOrganizationRequest);
            logger.info(showOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public ListRootsResponse listRoots(OrganizationsClient organizationsClient) {
        ListRootsRequest listRootsRequest = new ListRootsRequest();
        ListRootsResponse listRootsResponse = null;
        try {
            listRootsResponse = organizationsClient.listRoots(listRootsRequest);
            logger.info(listRootsResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
        return listRootsResponse;
    }

    public void deleteOrganization(OrganizationsClient organizationsClient) {
        DeleteOrganizationRequest deleteOrganizationRequest = new DeleteOrganizationRequest();
        try {
            DeleteOrganizationResponse deleteOrganizationResponse =
                organizationsClient.deleteOrganization(deleteOrganizationRequest);
            logger.info(deleteOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public CreateOrganizationalUnitResponse createOrganizationalUnit(OrganizationsClient organizationsClient,
                                                                     String parentId) {
        if (parentId == null || parentId.length() == 0) {
            return null;
        }
        CreateOrganizationalUnitRequest createOrganizationalUnitRequest = new CreateOrganizationalUnitRequest()
            .withBody(new CreateOrganizationalUnitReqBody().withName("{ou_name}").withParentId(parentId));  // parentId要在其中创建新组织单元的根或组织单元的唯一标识符;ou_name要分配给新组织单元的名称
        CreateOrganizationalUnitResponse createOrganizationalUnitResponse = null;
        try {
            createOrganizationalUnitResponse =
                organizationsClient.createOrganizationalUnit(createOrganizationalUnitRequest);
            logger.info(createOrganizationalUnitResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
        return createOrganizationalUnitResponse;
    }

    public void updateOrganizationalUnit(OrganizationsClient organizationsClient, String ouId) {
        if (ouId == null || ouId.length() == 0) {
            return;
        }
        UpdateOrganizationalUnitRequest updateOrganizationalUnitRequest = new UpdateOrganizationalUnitRequest()
            .withBody(new UpdateOrganizationalUnitReqBody().withName("{new_ou_name}"))  // new_ou_name:要分配给新组织单元的名称
            .withOrganizationalUnitId(ouId);  // ouId:与组织单元关联的唯一标识符
        try {
            UpdateOrganizationalUnitResponse updateOrganizationalUnitResponse =
                organizationsClient.updateOrganizationalUnit(updateOrganizationalUnitRequest);
            logger.info(updateOrganizationalUnitResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void showOrganizationalUnit(OrganizationsClient organizationsClient, String ouId) {
        if (ouId == null || ouId.length() == 0) {
            return;
        }
        ShowOrganizationalUnitRequest showOrganizationalUnitRequest =
            new ShowOrganizationalUnitRequest().withOrganizationalUnitId(ouId);  // ouId:与组织单元关联的唯一标识符
        try {
            ShowOrganizationalUnitResponse showOrganizationalUnitResponse =
                organizationsClient.showOrganizationalUnit(showOrganizationalUnitRequest);
            logger.info(showOrganizationalUnitResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void deleteOrganizationalUnit(OrganizationsClient organizationsClient, String ouId) {
        if (ouId == null || ouId.length() == 0) {
            return;
        }
        DeleteOrganizationalUnitRequest deleteOrganizationalUnitRequest =
            new DeleteOrganizationalUnitRequest().withOrganizationalUnitId(ouId);  // ouId:与组织单元关联的唯一标识符
        try {
            DeleteOrganizationalUnitResponse deleteOrganizationalUnitResponse =
                organizationsClient.deleteOrganizationalUnit(deleteOrganizationalUnitRequest);
            logger.info(deleteOrganizationalUnitResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    private static void LogError(ClientRequestException e) {
        logger.error("HttpStatusCode: " + e.getHttpStatusCode());
        logger.error("RequestId: " + e.getRequestId());
        logger.error("ErrorCode: " + e.getErrorCode());
        logger.error("ErrorMsg: " + e.getErrorMsg());
    }
}
